package Services

import (
	"errors"
	"minititok/Entities"
	"minititok/databases"
	"time"
)

func GetFiveCommentsByAvAndTime(av string, targetTime time.Time) ([]Entities.Comment, error) {
	var comments []Entities.Comment
	err := databases.DB.Where("av = ? AND created <= ? AND deleted = ?", av, targetTime, false).
		Order("created DESC").
		Limit(5).
		Find(&comments).
		Error
	return comments, err
}

func CreateNewComment(av, username, comment string) error {
	res := databases.DB.Create(&Entities.Comment{
		Av:       av,
		Username: username,
		Content:  comment,
	})
	if res.Error != nil {
		return errors.New("Failed to create new comment")
	}
	return nil
}
